package com.xxx.meter.ui.submit;

import android.content.res.Configuration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.base.BaseDialog;
import com.xxx.meter.model.http.Api;
import com.xxx.meter.model.http.ApiCallback;
import com.xxx.meter.model.http.bean.SubmitBean;
import com.xxx.meter.model.http.bean.base.BaseBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.ToastUtil;
import com.xxx.meter.ui.main.custom.MyRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class SubmitWindow extends BaseDialog implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.window_patrol_submit_Recycler)
    MyRecyclerView mRecycler;
    @BindView(R.id.window_patrol_submit_progress)
    ProgressBar mProgressBar;
    @BindView(R.id.window_patrol_submit_btn)
    Button mBtn;

    private int tag;

    private List<SubmitBean> mCheck = new ArrayList<>();
    private List<SubmitBean> mList = new ArrayList<>();
    private SubmitAdapter mAdapter;
    private Callback callback;
    private BaseActivity activity;
    private Disposable mDisposable;

    private boolean isRetry;

    public static SubmitWindow getInstance(BaseActivity activity, int tag, Callback callback) {
        return new SubmitWindow(activity, tag, callback);
    }

    private SubmitWindow(BaseActivity activity, int tag, Callback callback) {
        super(activity);
        this.activity = activity;
        this.tag = tag;
        this.callback = callback;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.window_patrol_submit;
    }

    @Override
    protected void initData() {
        mAdapter = new SubmitAdapter(mList);
        mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);

        //横屏设置最大200
        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mRecycler.setMaxHeight(500);
        }

        getValidationData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (ButtonUtil.isFastClick()) return;
        if (tag == 0) {
            mCheck.clear();
            mCheck.add(mList.get(position));
            mAdapter.setCheckData(mCheck);
        } else {
            boolean b = false;
            SubmitBean submitBean = null;
            String name = mList.get(position).getName();
            for (int i = 0; i < mCheck.size(); i++) {
                submitBean = mCheck.get(i);
                if (submitBean.getName().equals(name)) {
                    b = true;
                    break;
                }
            }
            if (b) {
                mCheck.remove(submitBean);
            } else {
                mCheck.add(mList.get(position));
            }
            mAdapter.setCheckData(mCheck);
        }
    }

    @OnClick({R.id.window_patrol_submit_btn})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.window_patrol_submit_btn:
                if (isRetry) {
                    if (mProgressBar != null) {
                        mProgressBar.setVisibility(View.VISIBLE);
                    }
                    mBtn.setText("提交");
                    getValidationData();
                } else {
                    if (callback != null) {
                        if (mCheck.size() != 0) {
                            callback.onResponse(mCheck);
                        } else {
                            ToastUtil.showToast("请选择操作人");
                        }
                    }
                }
                break;
        }
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
        super.dismiss();
    }

    public interface Callback {
        void onResponse(List<SubmitBean> list);
    }

    /**
     * 获取当前授权人数
     */
    private void getValidationData() {
        Observable.interval(0, 3, TimeUnit.SECONDS) //5秒请求一次
                .take(19)
                .flatMap(new Function<Long, ObservableSource<BaseBean<List<SubmitBean>>>>() {
                    @Override
                    public ObservableSource<BaseBean<List<SubmitBean>>> apply(Long count) {
                        return Api.getInstance().getValidationData();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ApiCallback<List<SubmitBean>>(activity, false) {

                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        mDisposable = d;
                    }

                    @Override
                    public void onSuccess(List<SubmitBean> list) {
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i).getTime() == null || list.get(i).getTime().isEmpty()) {
                                ToastUtil.showToast(list.get(i).getName());
                                dismiss();
                                return;
                            }
                        }
                        mList.clear();
                        mList.addAll(list);
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                        ToastUtil.showToast(errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        if (mProgressBar != null) {
                            mProgressBar.setVisibility(View.GONE);
                        }
                        if (mList == null || mList.size() == 0) {
                            mBtn.setText("重新获取");
                            isRetry = true;
                        }
                        mDisposable = null;
                    }
                });
    }
}
